//
//  AXFClickKeyWordViewController.m
//  AIXF
//
//  Created by Macx on 2017/7/10.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "AXFClickKeyWordViewController.h"
#import "AXFCollectionViewFlowLayout.h"
#import "AXFHomeCollectionViewCell.h"
#import "AXFHomeViewModel.h"
#import "AXFBottomSearchView.h"
#import <Masonry.h>
#import "AXFAccountManager.h"
#import "AXFShoppingCarViewController.h"
#import "AXFGoodsViewController.h"
@interface AXFClickKeyWordViewController ()<UICollectionViewDataSource>
@property (nonatomic,strong)NSArray <AXFHomeFirstSellModel *>*cellModelArray;
@property (nonatomic,strong)UICollectionView *collectionView;
@end

static NSString * const reuseIdentifier = @"Cell";
@implementation AXFClickKeyWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.cellModelArray = [AXFHomeViewModel shared].searchModelArray;
    
    AXFCollectionViewFlowLayout *flowLayout = [AXFCollectionViewFlowLayout new];
    
     UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    
    _collectionView = collectionView;
    
    [collectionView registerNib:[UINib nibWithNibName:@"AXFHomeCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:reuseIdentifier];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiceArray) name:@"postSearchArray" object:nil];
    
    collectionView.dataSource = self;
    
    AXFBottomSearchView *searchView = [[AXFBottomSearchView alloc] init];
    
    [self.view addSubview:collectionView];
    
    [self.view addSubview:searchView];
    
    [searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view);
        make.height.offset(60);
    }];
    
    UIButton *carBtn = (UIButton *)searchView.subviews.firstObject;
    
    [carBtn addTarget:self action:@selector(clickCarBtn) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)clickCarBtn
{
//    if (_AXFAccountManager.goodsNum == 0)
//    {
//        [self presentViewController:[AXFShoppingCarViewController new]  animated:YES completion:nil];
//    }else
//    {
//        [self.navigationController pushViewController:[AXFGoodsViewController new] animated:YES];
//    }
//    
   // [self.navigationController pushViewController:[AXFGoodsViewController new] animated:YES];
    
    [self presentViewController:[AXFGoodsViewController new] animated:NO completion:nil];
}


- (void)receiceArray
{
    self.cellModelArray = [AXFHomeViewModel shared].searchModelArray;
    
    [self.collectionView reloadData];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.cellModelArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AXFHomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    AXFHomeFirstSellModel *model = self.cellModelArray[indexPath.item];
    
    cell.model = model;
    
    cell.backgroundColor = [UIColor whiteColor];
    
    return cell;
}


@end
